package com.bike.controller.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bike.controller.BaseController;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhDining;
import com.bike.entity.HhFood;
import com.bike.service.impl.HhAgentServiceImpl;
import com.bike.service.impl.HhBusinessServiceImpl;
import com.bike.service.impl.HhBusinessUserServiceImpl;
import com.bike.util.Constant;
import com.bike.util.Md5Util;
import com.bike.util.PageData;
import com.bike.vo.PcJsonView;
import com.fasterxml.jackson.annotation.JsonView;

 
@Controller
@RequestMapping(value="/apiBusinessUser")
public class HhApiBusinessUserController extends BaseController {
	
		@Autowired
		private HhBusinessUserServiceImpl  hhBusinessUserServiceImpl;
		@Autowired
		private HhBusinessServiceImpl  hhBusinessServiceImpl;
	
		 /**
		  * 商家用户列表
		  * @param request
		  * @param model
		  * @return
		  */
		@RequestMapping("list")
		@ResponseBody
		@JsonView(PcJsonView.class)
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			pd.put("businessId", businessUser.getBusinessId());
			
			PageData rows=  hhBusinessUserServiceImpl.selectHhBusinessUserList(pd);
			PageData result=getSuccessPd(rows);
			return result;
		}
		/**
		 * 用户信息
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("userInfo")
		@ResponseBody
		public PageData userInfo(HttpServletRequest request,Model model){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
				PageData result=getSuccessPd();
				List<HhBusiness> businessList=new ArrayList<>();
				HhBusiness business=new HhBusiness();
				if(businessUser.getType()==Constant.unionType){//连锁店
					businessList=hhBusinessServiceImpl.selectAllByParentId(businessUser.getBusinessId());
				}else{
					business=hhBusinessServiceImpl.selectByHhBusinessId(businessUser.getBusinessId());
				}
				result.put("rows", businessList);
				result.put("data", businessUser);
				result.put("businessInfo", business);
				return result;
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			return  getErrorPd("密码错误");
		}
		/**
		 * 新增商家用户
		 * @param request
		 * @param name  桌牌名称
		 * @param businessId  商家id
		 * @param phone 手机号
		 * @param role  角色 boss   employ
		 * @param pwd  密码
		 * @return
		 */
		@RequestMapping("saveUser")
		@ResponseBody
		public PageData saveUser(HttpServletRequest request,HhBusinessUser hhBusinessUser,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			
//			if(pd.getLong("businessId")==businessUser.getBusinessId()){
				int result= hhBusinessUserServiceImpl.saveOrUpdate(pd, hhBusinessUser);
				if(result>0){
					return getSuccessPd();
				}else{
					return getErrorPd("参数错误,手机号不能为空");
				}
//			}
//			return getErrorPd("系统异常,请重试");
		}
	 
		/**
		 * 修改密码
		 * param  businessUserId
		 * param  pwd  旧密码
		 * param  newPwd 新密码
		 */
		@RequestMapping("updatePwd")
		@ResponseBody
		public PageData updatePwd(HttpServletRequest request,HhFood food,Model model){
			PageData pd=PageData.getToPageData(request);
			 
			HhBusinessUser hhBusinessUser=hhBusinessUserServiceImpl.selectByHhBusinessUserId(pd.getLong("businessUserId"));
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
//			if(hhBusinessUser!=null&&hhBusinessUser.getBusinessId().longValue()==businessUser.getBusinessId()){
				 
				if(hhBusinessUser.getPwd().equals(Md5Util.MD5(pd.getString("pwd")))){
					hhBusinessUser.setPwd(Md5Util.MD5(pd.getString("newPwd")));
				}
				int result= hhBusinessUserServiceImpl.saveOrUpdate(pd, hhBusinessUser);
				if(result<=0){
					return getSuccessPd();
				}
//			}
			return getErrorPd("系统异常,请重试");
		}
		/**
		 * 删除用户
		 * param  businessUserId
		 * param  pwd  旧密码
		 * param  newPwd 新密码
		 */
		@RequestMapping("deleteUser")
		@ResponseBody
		public PageData deleteUser(HttpServletRequest request,HhFood food,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhBusinessUser hhBusinessUser=hhBusinessUserServiceImpl.selectByHhBusinessUserId(pd.getLong("businessUserId"));
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
//			if(hhBusinessUser!=null&&hhBusinessUser.getBusinessId().longValue()==businessUser.getBusinessId()){
				HhBusinessUser deleteUser=new HhBusinessUser();
				deleteUser.setBusinessUserId(hhBusinessUser.getBusinessUserId());
				deleteUser.setStatus(Constant.delete);
				int result= hhBusinessUserServiceImpl.updateUser(pd, deleteUser);
				if(result>0){
					return getSuccessPd();
				}
//			}
			return getErrorPd("系统异常,请重试");
		}
}
